<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webxr/resources/webxr_util.js"></script>
<script src="/webxr/resources/webxr_test_constants.js"></script>

<script>
let testName = "inlineVerticalFieldOfView is set appropriately on inline sessions";

let fakeDeviceInitParams = VALID_NON_IMMERSIVE_DEVICE;

// This is an internal version of render_state_vertical_fov_inline that rather than
// checking that we clamp within the spec's range, verifies that chrome clamps
// to it's chosen min and max.
let minFOV = 0.01;
let maxFOV = 3.13;
let defaultFOV = Math.PI/2;

function assertApproximatelyEqual(a, b, step, epsilon = FLOAT_EPSILON) {
  assert_less_than(Math.abs(a - b), epsilon, step);
}

let testFunction = function(session, fakeDeviceController, t) {
  // Helper method because the renderState does not (per the spec) get updated
  // until the next rAF after it was updated, so this method returns a promise
  // which will resolve when the updated state should be applied.
  function updateAndApplyInlineFOV(fov) {
    session.updateRenderState({
      inlineVerticalFieldOfView: fov
    });

    return new Promise((resolve, reject) => {
      session.requestAnimationFrame(() => { resolve(); });
    });
  }

  // Helper method to keep the line length reasonable with a long attribute name
  // and ensure that the nullable value actually has a value.
  function getFOV() {
    let fov = session.renderState.inlineVerticalFieldOfView;
    t.step(() => {
      assert_not_equals(fov, null);
    });

    return fov;
  }

  return new Promise((resolve, reject) => {
      // Begin by validating that the default is set as expected/specced.
      t.step(() => {
        assertApproximatelyEqual(getFOV(), defaultFOV, "default");
      });

      // Set something below min, and assert that it is set to the min.
      updateAndApplyInlineFOV(-10).then(() => {

        t.step(() => {
          assertApproximatelyEqual(getFOV(), minFOV, "min");
        });

        // Set something above the max and assert that it is set to the max.
        updateAndApplyInlineFOV(10).then(()=> {
          t.step(()=> {
            assertApproximatelyEqual(getFOV(), maxFOV, "max");
          });

          // Set to something reasonable and assert that the value gets set.
          let normalFOV = 1.5;
          updateAndApplyInlineFOV(normalFOV).then(() => {
            t.step(() => {
              assertApproximatelyEqual(getFOV(), normalFOV, "normal");
            });

            resolve();
          });
        });
      });
  });
};

xr_session_promise_test(
  testName, testFunction, fakeDeviceInitParams, 'inline');

</script>
